﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace Feniks
{
    public class Database
    {
        public static string GetConnectionString()
        {
            string serverName = Program.Session[Settings.ServerName].ToString();
            string databaseName = Program.Session[Settings.DatabaseName].ToString();
            SqlConnectionStringBuilder sqlBuilder = new SqlConnectionStringBuilder();
            sqlBuilder.DataSource = serverName;
            sqlBuilder.InitialCatalog = databaseName;
            sqlBuilder.MultipleActiveResultSets = true;
            if (Program.Session[Settings.AuthenticateMode].ToString() == "Sql")
            {
                sqlBuilder.UserID = Program.Session[Settings.UserName].ToString();
                sqlBuilder.Password = Program.Session[Settings.Password].ToString();
                sqlBuilder.IntegratedSecurity = false;
            }
            else
                sqlBuilder.IntegratedSecurity = true;

            return sqlBuilder.ToString();
        }

        public static DataTable List(string sql)
        {
            DataTable dt = new DataTable();
            using (SqlConnection cn = new SqlConnection(GetConnectionString()))
            {
                cn.Open();
                using (SqlCommand cmd = new SqlCommand(sql, cn))
                {
                    using (SqlDataAdapter adp = new SqlDataAdapter(cmd))
                    {
                        adp.Fill(dt);
                    }
                }
                cn.Close();
            }
            return dt;
        }
    }
}
